/**
 * $Id$
 *
 * Unpublished Work Copyright 2013 Confirmit, Inc. All Rights Reserved.
 *
 * Last changed by $Author$
 * Last changed at $Date$
 */
package com.youtills.core.app.dev;

import com.youtills.core.app.common.ISiteLayout;
import com.youtills.core.app.common.SiteLayout;
import com.youtills.core.lang.StringUtil;
import com.youtills.core.web.IRequestHandler;
import org.apache.commons.codec.binary.Base64;

/**
 * TODO
 *
 * @author Ravi Kumar Chiluveru (RaviC)
 * @since 12.2, (7/10/13 5:17 PM)
 */
public class Base64RequestHandler extends SiteLayout implements IRequestHandler, ISiteLayout {

    public String getMainContentPage() {
        return "/WEB-INF/jsp/dev/base-64.jsp";
    }


    public String getSrc(){
        return getContextBean().getRequest().getParameter("src");
    }

    public String getResult(){
        String result = "";

        String action = getContextBean().getRequest().getParameter("action");
        String src = getSrc();

        if(!StringUtil.isEmpty(src)){
            try{
                if("Encode".equals(action)){
//                    Base64 encoder = new BASE64Encoder();
//                    result = encoder.encode(src.getBytes());
                    result = new String(Base64.encodeBase64(src.getBytes()));
                }else if ("Decode".equals(action)){
//                    BASE64Decoder decoder = new BASE64Decoder();
//                    result = new String(decoder.decodeBuffer(src));
                    result = new String(Base64.decodeBase64(src.getBytes()));
                }
            }catch (Exception e){
                result = "Exception " + e.getMessage();
            }
        }
        return result;
    }

}